// @algorithm @lc id=100302 lang=cpp
// @title bao-han-minhan-shu-de-zhan-lcof
#include <iostream>
#include <queue>
#include <stack>
#include <unordered_map>
using namespace std;

class MinStack {
private:
    priority_queue<int, vector<int>, greater<int>> minHeap;
    stack<int> s;
    unordered_map<int, int> hash;

public:
    MinStack() {}
    void push(int val) {
        s.push(val);
        minHeap.push(val);
        ++hash[val];
    }
    void pop() {
        int temp = s.top();
        s.pop();
        --hash[temp];
        if (hash[temp] == 0) {
            hash.erase(temp);
        }
    }
    int top() {
        return s.top();
    }
    int min() {
        int ret = minHeap.top();
        while (hash.count(ret) == 0) {
            minHeap.pop();
            ret = minHeap.top();
        }
        return ret;
    }
};